﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using HitachiService.DBAccess;
using HitachiService.Office;
using HitachiLib.Utils;

namespace HitachiService
{
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single, ConcurrencyMode=ConcurrencyMode.Multiple)]
    public class FloorService : IHitachiService
    {
        static object m_lock = new object();

        public ProjectData GetProjectById(int id)
        {
            return ProjectAccess.GetProjectById(id);
        }

        public List<ProjectData> GetProjectsByParentId(int parent_id)
        {
            return ProjectAccess.GetProjectsByParentId(parent_id);
        }

        public List<ProjectData> GetProjectsByResourceId(int resource_id)
        {
            return ProjectAccess.GetProjectsByResourceId(resource_id);
        }

        public List<ProjectData> GetProjectsByLevel(int level)
        {
            return ProjectAccess.GetProjectsByLevel(level);
        }

        public bool UpdateProject(ProjectData project_data)
        {
            return ProjectAccess.UpdateProject(project_data);
        }

        public bool UpdateProjects(List<ProjectData> project_data_list)
        {
            return ProjectAccess.UpdateProjects(project_data_list);
        }

        public bool AddProject(ProjectData project_data)
        {
            return ProjectAccess.AddProject(project_data);
        }

        public bool DeleteProject(ProjectData project_data)
        {
            return ProjectAccess.DeleteProject(project_data);
        }


        public ResourceData GetResourceById(int id)
        {
            return ResourceAccess.GetResourceById(id);
        }

        public List<ResourceData> GetResourcesByParentId(int parent_id)
        {
            return ResourceAccess.GetResourcesByParentId(parent_id);
        }

        public ResourceData GetResourceByUserPswd(String username, String password)
        {
            return ResourceAccess.GetResourceByUserPswd(username, password);
        }

        public bool UpdateResource(ResourceData resource_data)
        {
            return ResourceAccess.UpdateResource(resource_data);
        }

        public bool AddResource(ResourceData resource_data)
        {
            return ResourceAccess.AddResource(resource_data);
        }

        public bool DeleteResource(ResourceData resource_data)
        {
            return ResourceAccess.DeleteResource(resource_data);
        }

        public void DeleteScheduleById(int id)
        {
            ScheduleAccess.DeleteScheduleById(id);
        }

        public void SaveSchedule(ScheduleData data)
        {
            ScheduleAccess.SaveSchedule(data);
        }

        public void FreezeScheduleByProjectId(int project_id, int action_type)
        {
            ScheduleAccess.FreezeScheduleByProjectId(project_id, action_type);
        }

        public void FreezeSchedule(ScheduleData data)
        {
            ScheduleAccess.FreezeSchedule(data);
        }

        public List<ScheduleData> GetScheduleByProject(int project_id)
        {
            return ScheduleAccess.GetScheduleByProject(project_id);
        }

        public List<ScheduleData> GetSchedulesByParentProject(int parent_project_id)
        {
            return ScheduleAccess.GetSchedulesByParentProject(parent_project_id);
        }

        public SchedulePolicyData GetSchedulePolicyByProjectId(int project_id)
        {
            return SchedulePolicyAccess.GetSchedulePolicyByProjectId(project_id);
        }

        public void SaveSchedulePolicy(SchedulePolicyData data)
        {
            SchedulePolicyAccess.SaveSchedulePolicy(data);
        }

        public WholeLineData GetWholeLineData(int project_id)
        {
            return UtilsAccess.GetWholeLineData(project_id);
        }

        public WholeManageData GetWholeManageDataByResourceId(int resource_id)
        {
            return ProjectResourceAccess.GetWholeManageDataByResourceId(resource_id);
        }

        public ProjectResourceData SaveProjectResource(ProjectResourceData data)
        {
            return ProjectResourceAccess.SaveProjectResource(data);
        }

        public String GetScheduleDoc(int week_id, int line_id)
        {
            lock (m_lock)
            {
                try
                {
                    Office.ScheduleExporter exporter = new Office.ScheduleExporter(week_id, line_id);
                    return exporter.Export();
                }
                catch (Exception e)
                {
                    return e.Message;
                }
            }
        }
    }
}
